Add terminate selected tasks action in admin#449
Add terminate selected tasks action in admin#449caoqianming wants to merge 16 commits intocelery:mainfrom
Conversation
auvipy
left a comment
There was a problem hiding this comment.
can you please elaborate the change a bit more? can you also verify the change?
|
When a long task is in running state, I may need to terminate it for various reasons. So adding this action in admin is useful. I added the code and tested it and it works fine. In the beginning I was terminating tasks one by one in a for loop, later I realized that I could pass in a list, so I made some changes. |
auvipy
left a comment
There was a problem hiding this comment.
can you add tests to verify this? and do not introduce any regression?
|
Okay, I'll try. |
|
@caoqianming unittests are run using install a virtualenv with the test dependencies, make sure you are on python 3.11: This requires a postgres database to be available on Currently there are no tests testing the admin pages since there was very little functionality there, so you'll have to add a test_admin.py yourself. |
|
Hello. I add some test @AllexVeldman |
There was a problem hiding this comment.
Pull Request Overview
This PR adds a new action to the Django admin interface allowing administrators to terminate selected tasks, along with corresponding unit tests.
- Introduces terminate_task action in TaskResultAdmin which terminates tasks and reports success or error messages.
- Adds unit tests in t/unit/test_admin.py covering both successful termination and failure scenarios.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| t/unit/test_admin.py | Adds tests for the new terminate_task action |
| django_celery_results/admin.py | Implements the terminate_task action in the admin |
Comments suppressed due to low confidence (1)
t/unit/test_admin.py:27
- [nitpick] The variable name 'id' shadows the built-in function id(). Consider renaming it to 'task_id' or similar to avoid potential confusion.
id = uuid()
|
I am going to deeply consider it again, may be by the end of this month. |
auvipy
left a comment
There was a problem hiding this comment.
can you please fix the merge conflict on test file?
for more information, see https://pre-commit.ci
…tion definition and remove trailing blank line at end of file
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
django_celery_results/admin.py
Outdated
| self.message_user( | ||
| request, | ||
| f"{len(task_ids)} Task was terminated successfully.", | ||
| messages.SUCCESS, | ||
| ) |
There was a problem hiding this comment.
The success message is grammatically incorrect for counts != 1 (e.g., "2 Task was...") and is not translatable. Consider using Django’s pluralization utilities (e.g., ngettext) and wrapping the message in translations so admin UI strings are localized consistently with the rest of this module.
django_celery_results/admin.py
Outdated
| except Exception as e: | ||
| self.message_user( | ||
| request, | ||
| f"Error while terminating tasks: {e}", | ||
| messages.ERROR, | ||
| ) |
There was a problem hiding this comment.
Catching Exception and interpolating the raw exception into a user-facing admin message can leak internal details. Prefer logging the exception (with stack trace) and showing a generic failure message, or at least sanitizing/normalizing the error output for admins.
There was a problem hiding this comment.
please check this @caoqianming if this is right or not
There was a problem hiding this comment.
add err log, but still keep err message
django_celery_results/admin.py
Outdated
| messages.ERROR, | ||
| ) | ||
|
|
||
| terminate_task.short_description = "Terminate selected tasks" |
There was a problem hiding this comment.
terminate_task.short_description is a user-facing admin string but it isn’t wrapped in gettext_lazy (the module already uses _() for other UI text). Consider making this translatable to keep admin UI localization consistent.
t/unit/test_admin.py
Outdated
| @patch('celery.current_app.control.terminate') | ||
| def test_terminate_task_success(self, mock_terminate): |
There was a problem hiding this comment.
The patch target should generally point at where the symbol is used. Since terminate_task() calls django_celery_results.admin.celery_app.control.terminate, patching celery.current_app.control.terminate may not reliably intercept the call (depending on how Celery’s current_app proxy is resolved). Patch the admin module reference instead to make the test robust.
| self.assertEqual( | ||
| str(messages[0]), | ||
| "2 Task was terminated successfully.") | ||
| self.assertEqual(messages[0].level, constants.SUCCESS) |
There was a problem hiding this comment.
The expected success message asserts the same singular/plural grammar issue as the implementation ("2 Task was..."). Once the admin message is fixed to pluralize correctly, this assertion should be updated accordingly (and ideally made resilient using pluralization utilities).
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
for more information, see https://pre-commit.ci

No description provided.